Engine = require "engine.package"
Offset = require "offset"
ClassObject = require "classObject"

--custom目录下的文件不会强制覆盖,可以自行修改
--如果想要更新官方的custom文件,可以在登录器中勾选强制覆盖更新
ObjectInfo = require "custom.objectInfo"
GatherableInfo = require "custom.gatherableInfo"
Menu = require "custom.menu"

---主线程回调
---meta.engine中的函数只能在该函数内调用,否则会导致游戏崩溃
function lua_main()
	Menu.Menu()
	local AcknowledgedPawn = GetAcknowledgedPawn(1)
	local MeLocation = GetActorLocation(AcknowledgedPawn)
	local actors = GetAllActorsOfClass(ClassObject.PlayerCharacter)
	if actors then
		---@param actorPtr BaseCharacter
		for _, actorPtr in ipairs(actors) do
			if actorPtr then
				if actorPtr == AcknowledgedPawn then goto continue end
				--获取单位名称
				local character_name = GetCharacterName(actorPtr)
				--初始化单位信息
				local Info = {
					Text = character_name,
					Color = Engine.ToColor(255, 0, 255, 125),
					FontSize = 12,
					Filter = false,
				}
				--加载单位信息
				if ObjectInfo[character_name] then
					Info = ObjectInfo[character_name]
					if Info.Filter then
						goto continue
					end
					Info.Text = Info.Text or character_name
					Info.Color = Info.Color or Engine.ToColor(255, 125, 0, 255)
					Info.FontSize = Info.FontSize or 12
				end
				--获取单位坐标
				local location = GetActorLocation(actorPtr)
				local distance = GetDistance(MeLocation, location)
				local screenPos = { x = 0, y = 0 }
				--转换坐标到屏幕
				if WorldToScreen(location, screenPos) then
					--画单位名称
					Draw_Text(screenPos.x, screenPos.y, Info.FontSize, Info.Color, Info.Text)
					Draw_Text(screenPos.x, screenPos.y + 18, 10, Engine.ToColor(255, 255, 255, 255),
						string.format("%.0fm", distance))
				end
			end
			::continue::
		end
	end

	actors = GetAllActorsOfClass(ClassObject.NPCCharacter)
	if actors then
		---@param actorPtr BaseCharacter
		for i, actorPtr in ipairs(actors) do
			if actorPtr then
				--获取单位坐标
				local location = GetActorLocation(actorPtr)
				local distance = GetDistance(MeLocation, location)
				if distance > 300 then
					goto continue
				end
				--判断单位是否死亡,死亡则过滤
				if IsDead(actorPtr) then goto continue end
				--获取单位名称
				local character_name = GetCharacterName(actorPtr)
				--初始化单位信息
				local Info = {
					Text = character_name,
					Color = Engine.ToColor(255, 125, 0, 255),
					FontSize = 12,
					Filter = false,
				}
				--加载单位信息
				if ObjectInfo[character_name] then
					Info = ObjectInfo[character_name]
					if Info.Filter then
						goto continue
					end
					Info.Text = Info.Text or character_name
					Info.Color = Info.Color or Engine.ToColor(255, 125, 0, 255)
					Info.FontSize = Info.FontSize or 12
				end
				local IsEnemy = IsEnemy(actorPtr, AcknowledgedPawn)
				if not IsEnemy then
					Info.Color = Engine.ToColor(0, 255, 0, 255)
				elseif not Menu["显示敌对"] then
					goto continue
				end
				-- if ReadBool(actorPtr + Offset.NPCCharacter.bIsPet) then
				-- 	if not Menu["显示宠物"] then
				-- 		goto continue
				-- 	end
				-- end
				local screenPos = { x = 0, y = 0 }
				--转换坐标到屏幕
				if WorldToScreen(location, screenPos) then
					--画单位名称
					Draw_Text(screenPos.x, screenPos.y, Info.FontSize, Info.Color, Info.Text)
					Draw_Text(screenPos.x, screenPos.y + 18, 10, Engine.ToColor(255, 255, 255, 255),
						string.format("%.0fm", distance))
				end
			end
			::continue::
		end
	end

	actors = GetAllActorsOfClass(ClassObject.InteractableStateMachineBase)
	if actors then
		for _, actorPtr in ipairs(actors) do
			if actorPtr then
				--获取单位坐标
				local location = GetActorLocation(actorPtr)
				local distance = GetDistance(MeLocation, location)
				if distance > 300 then
					goto continue
				end
				local actor_name = GetActorName(actorPtr)
				local Info = {
					Text = actor_name,
					Color = Engine.ToColor(125, 0, 255, 255),
					FontSize = 12,
					Filter = false,
				}
				--加载单位信息
				if ObjectInfo[actor_name] then
					Info = ObjectInfo[actor_name]
					if Info.Filter then
						goto continue
					end
					Info.Text = Info.Text or actor_name
					Info.Color = Info.Color or Engine.ToColor(125, 0, 255, 255)
					Info.FontSize = Info.FontSize or 12
				end
				local screenPos = { x = 0, y = 0 }
				--转换坐标到屏幕
				--if WorldToScreen(location, screenPos) then
				--画单位名称
				--Draw_Text(screenPos.x, screenPos.y, 12, Engine.ToColor(255, 0, 255, 255), actor_name)
				--Draw_Text(screenPos.x, screenPos.y + 18, 10, Engine.ToColor(255, 255, 255, 255),
				--string.format("%.0fm", distance))
				--end
			end
			::continue::
		end
	end

	actors = GetAllActorsOfClass(ClassObject.GatherableActor)
	if actors then
		for _, actorPtr in ipairs(actors) do
			if actorPtr then
				--过滤已采集的采集物
				if ReadBool(actorPtr + Offset.GatherableActor.bGathered) then
					goto continue
				end
				--获取单位坐标
				local location = GetActorLocation(actorPtr)
				local distance = GetDistance(MeLocation, location)
				if distance > 300 then
					goto continue
				end
				local screenPos = { x = 0, y = 0 }
				--转换坐标到屏幕
				if WorldToScreen(location, screenPos) then
					--画单位名称
					local actor_name = GetGatherableActorName(actorPtr)
					if actor_name then
						if GatherableInfo[actor_name] then
							Draw_Text(screenPos.x, screenPos.y, 12, GatherableInfo[actor_name].Color, GatherableInfo[actor_name].Name)
						else
							Draw_Text(screenPos.x, screenPos.y, 12, Engine.ToColor(255, 255, 255, 255), actor_name)
						end
					else
						Draw_Text(screenPos.x, screenPos.y, 12, Engine.ToColor(0, 255, 255, 255), "采集物")
					end
					Draw_Text(screenPos.x, screenPos.y + 18, 10, Engine.ToColor(255, 255, 255, 255),
						string.format("%.0fm", distance))
				end
			end
			::continue::
		end
	end
end
